﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LX.Core.Query;

namespace System.Linq
{
    public static class EntityFilterExtend
    {
        public static List<T> FilterBy<T>(this IQueryable<T> query, EntityFilter<T> filter)
        {
            if (filter == null)
            {
                return query.ToList();
            }
            return filter.Filter(query).ToList();
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> query, EntityFilter<T> filter)
        {
            if (filter == null)
            {
                int total = query.Count();
                return new PagedList<T>
                {
                    Data = query.ToList(),
                    Total = total,
                    Page = 1,
                    PageSize = total
                };
            }
            var count = filter.Count(query);
            var data = filter.Filter(query).ToList();
            var page = 1;
            var pageSize = count;
            if (filter.PageCodition != null)
            {
                page = filter.PageCodition.Page;
                pageSize = filter.PageCodition.PageSize;
            }
            return new PagedList<T>
            {
                Data = data,
                Total = count,
                Page = page,
                PageSize = pageSize
            };
        }
    }
}
